package com.elitel.frame.web.serverreqstat.controller;

import com.elitel.common.core.domain.JsonResult;
import com.elitel.frame.web.common.controller.ApiController;
import com.elitel.frame.web.serverreqstat.dto.ServerReqStatDTO;
import com.elitel.frame.web.serverreqstat.entity.ServerReqSimpleStat;
import com.elitel.frame.web.serverreqstat.service.ServerReqStatService;
import com.elitel.frame.web.sysreqlog.entity.SysReqDetail;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author m
 */
@RestController("serverReqStatController")
@RequestMapping("sysReqLogStat")
public class ServerReqStatController extends ApiController {

    @Resource
    private ServerReqStatService serverReqStatService;


    /**
     * 获取服务请求统计信息
     *
     * @param dto
     * @return
     */
    @GetMapping("getServerReqSimpleStat")
    public JsonResult<?> getServerReqSimpleStat(ServerReqStatDTO dto) {
        ServerReqSimpleStat serverReqSimpleStat = serverReqStatService.getServerReqSimpleStat(dto);
        return JsonResult.success(serverReqSimpleStat);
    }

    /**
     * 获取服务错误统计信息
     *
     * @param dto
     * @return
     */
    @GetMapping("getServerErrorStat")
    public JsonResult<?> getServerErrorStat(ServerReqStatDTO dto) {
        return JsonResult.success(serverReqStatService.getServerErrorStat(dto));
    }

    /**
     * 获取服务调用方统计信息
     *
     * @param dto
     * @return
     */

    @GetMapping("getServerAppStat")
    public JsonResult<?> getServerAppStat(ServerReqStatDTO dto) {
        return JsonResult.success(serverReqStatService.getServerAppStat(dto));
    }

    /**
     * 获取服务统计列表信息
     *
     * @param dto
     * @return
     */
    @GetMapping("getServerStatList")
    public JsonResult<?> getServerStatList(ServerReqStatDTO dto) {
        return getDataTable(serverReqStatService.getServerStatList(dto));
    }

    @GetMapping("pageServerStatListInfo")
    public JsonResult<?> pageServerStatListInfo(String startDate, String endDate, Integer days,
                                                Integer pageNum, Integer pageSize,
                                                Integer respStatus,
                                                Integer respTime,
                                                Integer isTimeOut,
                                                String servName) throws ParseException {
        PageInfo<SysReqDetail> pageInfo = serverReqStatService.pageServerStatListInfo(startDate, endDate, days, pageNum,
                pageSize, servName, respStatus, respTime, isTimeOut);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("rows", pageInfo.getList());
        return JsonResult.success(map);
    }


    /**
     * 获取服务统计列表信息
     *
     * @return
     */
    @GetMapping("pullSaveStatLog")
    public JsonResult<?> pullSaveStatLog() {
        // 以下两个操作不能在一个事务中
        serverReqStatService.pullSaveLog();
        serverReqStatService.statSaveLog();
        return JsonResult.success();
    }

    @GetMapping("statSaveStatLog")
    public JsonResult<?> statSaveStatLog() {
        serverReqStatService.statSaveLog();
        return JsonResult.success();
    }

    @GetMapping("changeSubStrFlag")
    public JsonResult<?> changeSubStrFlag() {
        boolean flag = serverReqStatService.changeSubStrFlag();
        if (!flag) {
            return JsonResult.success("日志开始记录全量参数");
        } else {
            return JsonResult.success("日志开始记录部分参数");
        }
    }


    @GetMapping("delDetailTask")
    public JsonResult<?> delDetailTask() {
        LocalDateTime localDateTime = LocalDateTime.now().minusDays(30);
        serverReqStatService.delDetailsByReqDate(localDateTime);
        return JsonResult.success();
    }
}
